package com.huatai.bi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.bi.entity.TableEntity;
import com.huatai.bi.query.TableQuery;
import com.huatai.bi.service.TableService;
import com.huatai.common.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "表信息",tags = "数据库连接表",position = 1)
@RequestMapping("/table")
public class TableController {

    @Autowired
    TableService service;

    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据ID获取数据")
    public R<TableEntity> get(@PathVariable("id") Long id){
        TableEntity entity = service.getById(id);
        if(null == entity){
            return R.fail("获取数据失败，记录不存在");
        }
        service.buildExInfo(entity);
        return R.data(entity);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取数据-分页")
    public R<Page> Page(@RequestBody TableQuery query){
        return R.data(service.page(query));
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取列表")
    public R<List<TableEntity>> list(@RequestBody TableQuery query){
        return R.data(service.list(query));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增或修改")
    public R save(@RequestBody TableEntity entity){
        return R.data(service.saveOrUpdate(entity));
    }

}
